{{ define "RenderImageSimple" -}}
  <img class="my-0 rounded-md" loading="lazy" alt="{{ .alt }}" src="{{ .src }}">
{{- end }}

{{ define "RenderImageResponsive" -}}
  <img
    class="my-0 rounded-md"
    loading="lazy"
    decoding="async"
    fetchpriority="low"
    alt="{{ .alt }}"
    srcset="
      {{ (.resource.Resize "330x").RelPermalink }}  330w,
      {{ (.resource.Resize "660x").RelPermalink }}  660w,
      {{ (.resource.Resize "1280x").RelPermalink }} 1280w
    "
    data-zoom-src="{{ .resource.RelPermalink }}"
    src="{{ .resource.RelPermalink }}">
{{- end }}

{{ define "RenderImageCaption" -}}
  {{- with .caption }}
    <figcaption>{{ . | markdownify }}</figcaption>
  {{- end }}
{{- end }}

{{- $disableImageOptimizationMD := .Page.Site.Params.disableImageOptimizationMD | default false }}
{{- $urlStr := .Destination | safeURL -}}
{{- $url := urls.Parse $urlStr -}}
{{- $altText := .Text }}
{{- $caption := .Title }}
{{- $isRemote := findRE "^(https?|data)" $url.Scheme }}
{{- $resource := "" }}

{{- if not $isRemote }}
  {{- $resource = or ($.Page.Resources.GetMatch $urlStr) (resources.Get $urlStr) }}
{{- end }}


<figure>
  {{- if $isRemote }}
    {{ template "RenderImageSimple" (dict "src" $urlStr "alt" $altText) }}
  {{- else if $resource }}
    {{- $isSVG := eq $resource.MediaType.SubType "svg" }}
    {{- $shouldOptimize := and (not $disableImageOptimizationMD) (not $isSVG) }}
    {{- if $shouldOptimize }}
      {{ template "RenderImageResponsive" (dict "resource" $resource "alt" $altText) }}
    {{- else }}
      {{ template "RenderImageSimple" (dict "src" $resource.RelPermalink "alt" $altText) }}
    {{- end }}
  {{- else }}
    {{ template "RenderImageSimple" (dict "src" $urlStr "alt" $altText) }}
  {{- end }}

  {{ template "RenderImageCaption" (dict "caption" $caption) }}
</figure>
